import os
import pickle

class Shouter(object):
    """
    An independent honor lister class (because separation of application
    and protocol logic is a good thing).
    """
    triggers = ["!shout", "!setshout"]
    prefix = "http://u1.swirve.com/players/honor-"
    postfix = ".htm"

    name = "shout"
    help = ["Bragging and other verbal abuse",
            "%c6!shout%c <message>"%(03,03),
            "%c6!setshout%c <on/off>"%(03,03)]
            
    def __init__(self):
        self.dontshout = set([])
        self.filename = ".\\dontshout.txt"
        if os.path.exists(self.filename):
            f = open(self.filename, 'r')
            self.dontshout = pickle.load(f)
            f.close()
    
    def save(self):
        f = open(self.filename, 'w')
        pickle.dump(self.dontshout, f)
        f.close()
        
    def setShout(self, channel, shout):
        if shout == True:
            self.dontshout.discard(channel)
        else:
            self.dontshout.add(channel)
        self.save()
    
    def command(self, firstword, user, channel, msg):
        parts = msg.split()
        
        if parts[0] == "!shout":
            if channel in self.dontshout:
                return {'chanmsg': ["Shouting is currently disabled in this channel."]}
            prefix = "%c10 %s%c in%c6 %s%c shouts: "%(03,user,03,03,channel,03)
            shout = prefix + " ".join(parts[1:])
            return {'shoutmsg': (shout, self.dontshout)}
        
        if parts[0] == "!setshout":
            if len(parts) != 2 or parts[1] not in ["on","off"]:
                return {'chanmsg': ["Usage: !setshout [on/off]"]}
            if parts[1] == "on":
                self.setShout(channel, True)
                return {'chanmsg': ["Channel shouting switched%c4 on%c."%(03,03)]}
            else:
                self.setShout(channel, False)
                return {'chanmsg': ["Channel shouting switched%c4 off%c."%(03,03)]}
                
        
